translationHost
The translationHost view modifier is used to provide a translation service context to your UI. This modifier enables user interaction with system-level translation dialogs, such as downloading required languages or selecting ambiguous source languages.
Purpose
Apply the translationHost modifier to the root view of your page when using the Translation class for localized translations. It ensures that:
- If the source or target language is not currently available on the device, the system will prompt the user to download the necessary language resources.
- If the source language is not specified and cannot be inferred from the text content, the system will prompt the user to select a source language.
Without applying this modifier, such user prompts may not function correctly, and your translation session may fail silently or throw an error.
Type
The value of this modifier must be an instance of the Translation class.
Usage Example
In this example:
- A
Translationinstance is created usinguseMemo. - A batch of English texts is translated to French.
- The
VStackview is wrapped with thetranslationHostmodifier so the system can show download or language selection prompts if necessary.
Best Practices
- Always apply
translationHostto the top-level container view when performing translation-related operations. - Use a consistent
Translationinstance that matches the one used for calling.translate()or.translateBatch(). - Avoid creating multiple
Translationinstances for the same session if possible.
